package cn.wikiflyer.beikang.act.user;

import java.lang.reflect.Type;

import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import cn.wikiflyer.beikang.BaseActivity;
import cn.wikiflyer.beikang.R;
import cn.wikiflyer.beikang.act.UserInfoBean;
import cn.wikiflyer.beikang.bean.BalanceListBean;
import cn.wikiflyer.beikang.bean.BalanceListBean.BalanceBean;
import cn.wikiflyer.beikang.bean.TixianListBean;
import cn.wikiflyer.beikang.bean.TixianListBean.TixianBean;
import cn.wikiflyer.beikang.bean.base.BKBaseBean;
import cn.wk.libs4a.utils.WKTimeUtils;
import cn.wk.libs4a.view.WKHeader;
import cn.wk.libs4a.view.list.WKListViewInterface;
import cn.wk.libs4a.view.list.WKListViewUtils;
import cn.wk.libs4a.view.list.WKViewHolder;
import cn.wk.libs4a.view.pulltorefresh.PullToRefreshListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.umeng.socialize.utils.Log;

/**
 * 提现历史
 */
public class UserTiXianListAct extends BaseActivity implements
		WKListViewInterface {

	@ViewInject(id = R.id.header)
	private WKHeader header;

	private TextView center_cash;
	private TextView center_balance;
	private TextView center_unclear;

	private WKListViewUtils<TixianBean, TixianListBean> listUtils;
	private UserInfoBean mUserBean;

	@Override
	public void getIntentData(Bundle savedInstanceState) {
		super.getIntentData(savedInstanceState);
		mUserBean = (UserInfoBean) getIntent().getSerializableExtra("bean");
	}

	@Override
	public void loadXml() {
		setContentView(R.layout.bk_act_tixian);
	}

	@Override
	public void init() {
		header.setLeftImg("提现", R.drawable.user_outmoney_img, false, 12);
		header.setClickListener(null, null, new OnClickListener() {
			public void onClick(View v) {
				startActivity(new Intent(ctx, UserBalanceCashAct.class));
			}
		});

		View header_view = LayoutInflater.from(ctx).inflate(
				R.layout.bk_header_user_balance_list, null);
		center_cash = (TextView) header_view.findViewById(R.id.center_cash);
		center_balance = (TextView) header_view
				.findViewById(R.id.center_balance);
		center_unclear = (TextView) header_view
				.findViewById(R.id.center_unclear);

		PullToRefreshListView pList = (PullToRefreshListView) findViewById(R.id.wk_pull_refresh_list);
		listUtils = new WKListViewUtils<TixianBean, TixianListBean>() {
		};
		listUtils.init(ctx, this, pList, header_view);
		if (mUserBean != null) {
			if (mUserBean.history_cash.equals("0")) {
				center_cash.setText("0.00");
			} else {
				center_cash.setText("" + mUserBean.history_cash);
			}
			center_unclear.setText("" + mUserBean.amount);

			try {
				float m = Float.valueOf(mUserBean.money);
				float c = Float.valueOf(mUserBean.history_cash);
				if (mUserBean.sales_cash.equals("0")) {
					center_balance.setText("0.00");
				} else {
					center_balance.setText("" + mUserBean.sales_cash);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public void setData() {
		listUtils.updateData();
	}

	@Override
	protected void onRestart() {
		super.onRestart();
		listUtils.updateData();
	}

	@Override
	public String getData(boolean... isLocal) {
		return app().net.tixian(isLocal);
	}

	@Override
	public void getDataBack(String result, boolean isLocal) {
		Type cvbType = new TypeToken<BKBaseBean<TixianListBean>>() {
		}.getType();
		Gson gson = new Gson();
		// 使用数据
		try {
			listUtils.bean = gson.fromJson(result, cvbType);
			listUtils.list.addAll(listUtils.bean.data.list);
			listUtils.adapter.notifyDataSetChanged();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public WKViewHolder createViewHolder(View arg1) {
		return new MyViewHolder(arg1);
	}

	@Override
	public int getItemViewId() {
		return R.layout.bk_act_user_income_item;
	}

	// -1 失败 0 未处理，2 成功
	class MyViewHolder extends WKViewHolder<TixianBean> {
		TextView time;
		TextView cardno;
		TextView count;
		TextView other;

		public MyViewHolder(View view) {
			time = (TextView) view.findViewById(R.id.time);
			cardno = (TextView) view.findViewById(R.id.cardno);
			count = (TextView) view.findViewById(R.id.count);
			other = (TextView) view.findViewById(R.id.other);
		}

		@Override
		public void setData(final TixianBean t) {
			time.setText(WKTimeUtils.formatTime(t.apply_time));
			cardno.setText("" + t.cardNumber);
			count.setText("" + t.money);
			if (t.apply_status != null) {
				if (t.apply_status.equals("-1")) {
					other.setText("提现失败");
				} else if (t.apply_status.equals("0")) {
					other.setText("未处理");
				} else if (t.apply_status.equals("2")) {
					other.setText("提现成功");
				}
			}
			// proText.setText(t.type);
			// countText.setText(t.money + "");
			// markText.setText(t.buyer);
		}
	}

}
